/*****************************************************************************
*Purpose: Estimate Impacts on Initial Neighborhood (Neighborhoods and Schools)
******************************************************************************/ 

global fe "mitrct80 intakeyear birthyear"
global controls "female head_female headage married auto license work emp inc ph i.bed"


*** Impact on Initial Neighborhood Placement

capture frame drop firststage 
frame create firststage str35 group str35 spec beta se str10 signf str55 outcome N pval


foreach spec in base nocont notractfe meansd { 
foreach out in place_pctblack place_pctwhite place_povrate place_incrnk sch_black_share_8788_pl sch_white_share_8788_pl totalexp_perpupil_91_pl pupils_perteacher_8687_pl {

		reghdfe place_pctblack general_area $controls if estsamp==1, absorb($fe) cluster(hhid) 
			

		if "`spec'"=="notractfe" { 
			reghdfe `out' general_area if  estsamp==1 , absorb(intakeyear birthyear) cluster(hhid)
			test general_area==0
			frame post firststage ("Diff") ("`spec'") (_b[general_area]) (_se[general_area]) ("***") ("`out'")  (e(N)) (r(p))
		} 

		
		if "`spec'"=="nocont" { 
			reghdfe `out' general_area if estsamp==1 , absorb($fe) cluster(hhid) 
			test general_area==0
			frame post firststage ("Diff") ("`spec'") (_b[general_area]) (_se[general_area]) ("***") ("`out'")  (e(N)) (r(p))
		} 

		if "`spec'"=="base" { 
			reghdfe `out' general_area $controls if estsamp==1, absorb($fe) cluster(hhid) 
			test general_area==0
			frame post firststage ("Diff") ("`spec'") (_b[general_area]) (_se[general_area]) ("***") ("`out'")  (e(N)) (r(p))
		} 

		if "`spec'"=="meansd" { 
			sum  `out' if general_area==0 &  estsamp==1
			frame post firststage ("Meansd") ("`spec'") (r(mean)) (r(sd)) ("***") ("`out'")  (r(N)) (.)

		} 

}
}



***************************
******** Table  X *********
***************************
frame change firststage
gen stars = "*" if pval <= 0.1 & pval > 0.05
replace stars = "**" if pval <= 0.05 & pval > 0.01
replace stars = "***" if pval <= 0.01

** drop stuff i dont need 
*keep if inlist(group, "all")
keep if inlist(outcome,  "place_pctblack", "place_pctwhite", "place_povrate", "place_incrnk", "sch_black_share_8788_pl", "sch_white_share_8788_pl", "totalexp_perpupil_91_pl",  "pupils_perteacher_8687_pl")
*drop if inlist(outcome, "m2010_incrnk_black", "place_incrnk_black", "m26_incrnk_black", "m28_incrnk_black", "sametractparent2019")
keep if inlist(spec, "meansd", "notractfe", "nocont", "base")
keep if group!="MTO"

replace outcome="class_size" if outcome=="pupils_perteacher_8687_pl"
replace outcome="spend" if outcome=="totalexp_perpupil_91_pl"
replace outcome="schoolblack" if outcome=="sch_black_share_8788_pl"
replace outcome="schoolwhite" if outcome=="sch_white_share_8788_pl"

** defining locals in loops 
levelsof outcome, local(outcomes)
levelsof spec, local(specs)
levelsof beta, local(beta)
levelsof se, local(serror)
levelsof stars, local(stars)




	// beta 
foreach outx in `outcomes' {
	local id1 = "`outx'"
	
	foreach spex in `specs' {
		local id2 = "`spex'"
		
				
				sum beta if outcome == "`outx'" & ///
							spec == "`spex'"
				local `id1'_`id2'_b = string(r(mean), "%9.3fc")
				if outcome=="spend" {
					local `id1'_`id2'_b = string(r(mean), "%9.0fc")
				}
				
				if outcome=="class_size" {
					local `id1'_`id2'_b = string(r(mean), "%9.1fc")
				}
		
				sum se if 	outcome == "`outx'" & ///
							spec == "`spex'" 
				local 	`id1'_`id2'_se = string(r(mean),"%9.3fc")
				if outcome=="spend" {
					local `id1'_`id2'_se = string(r(mean), "%9.0fc")
				}
				if outcome=="class_size" {
					local `id1'_`id2'_se = string(r(mean), "%9.1fc")
				}
								
				sum N if outcome == "`outx'" & ///
								 spec == "`spex'"
				local `id1'_`id2'_n = string(r(mean), "%9.0fc")
								
				levelsof stars if outcome == "`outx'" & ///
									spec == "`spex'" , ///
				clean local(`id1'_`id2'_str)
				di "`id1'_`id2'_str"		
			}
}
	
	*tex \resizebox{\textwidth}{!}{

*****************************
******** First Stage ********
*****************************


texdoc init "$results\Table_2.tex", replace force
tex \begin{tabular}{l C{2.35cm} C{2.35cm} C{2.35cm} C{2.35cm}} 
tex \toprule 
tex & (1) &  (2) & (3) & (4) \\ \\[-1.75ex]
tex &\multicolumn{4}{c}{Placement Tract Characteristic} \\ \\[-2.5ex]
tex \cmidrule{2-5} \\[-1.75ex]
tex &\shortstack{Share \\ \\ Black} & \shortstack{Share \\ \\ White} & \shortstack{Poverty \\ \\ Rate} & \shortstack{Later-life \\ \\ Income Rank} \\ \\[-2.5ex]
tex \midrule
tex $1(\mathit{Share Black}_{d(i)} < 0.30)$ & `place_pctblack_base_b'`place_pctblack_base_str' & `place_pctwhite_base_b'`place_pctwhite_base_str' & `place_povrate_base_b'`place_povrate_base_str' & `place_incrnk_base_b'`place_incrnk_base_str' \\
&(`place_pctblack_base_se') & (`place_pctwhite_base_se') & (`place_povrate_base_se') & (`place_incrnk_base_se') \\\\
tex Control Mean & `place_pctblack_meansd_b'`place_pctblack_meansd_str' & `place_pctwhite_meansd_b'`place_pctwhite_meansd_str' & `place_povrate_meansd_b'`place_povrate_meansd_str' & `place_incrnk_meansd_b' \\
tex Sample Size & `place_pctblack_base_n'  & `place_pctwhite_base_n'  & `place_povrate_base_n'  & `place_incrnk_base_n'  \\
tex \bottomrule
tex \end{tabular}



*****************************
******** School Qual ********
*****************************

texdoc init "$results\Supplementary_Table_3.tex", replace force
tex \begin{tabular}{l C{2.35cm} C{2.35cm} C{2.35cm} C{2.35cm}} 
tex \toprule 
tex & (1) &  (2) & (3) & (4) \\ \\[-1.75ex]
tex &\multicolumn{4}{c}{\shortstack{Placement Neighborhood \\ \\ School Characteristic}} \\ \\[-2.5ex]
\cmidrule{2-5} \\[-1.75ex]
tex &\shortstack{School: \\ \\ Share \\ \\ Black} & \shortstack{School: \\ \\ Share \\ \\ White} & \shortstack{District \\ \\ Spending \\ \\ Per Pupil} & \shortstack{Class Size \\ \\ (Students \\ \\ Per Teacher)} \\ \\[-2.5ex]
tex \midrule
tex $ 1(\mathit{Share Black_{d(i)}} < 0.30)$  & `schoolblack_base_b'`schoolblack_base_str' & `schoolwhite_base_b'`schoolwhite_base_str'  & `spend_base_b'`spend_base_str' & `class_size_base_b'`class_size_base_str' \\
tex & (`schoolblack_base_se') & (`schoolwhite_base_se') & (`spend_base_se') & (`class_size_base_se') \\\\
tex Control Mean & `schoolblack_meansd_b' & `schoolwhite_meansd_b' & `spend_meansd_b' & `class_size_meansd_b' \\
tex Sample Size & `schoolblack_base_n' & `schoolwhite_base_n' & `spend_nocont_n' & `class_size_base_n' \\
tex \bottomrule
tex \end{tabular}

*******************************
****** Nhood Correlations *****
*******************************

** Correlation matrix
correl pctwhtp place_povrate place_medhhincome place_wba place_unemplyr if estsamp==1 




* create matrix frame 
frame create corr_matrix str32(neigh_char) 

matrix accum R = pctwhtp place_povrate place_medhhincome place_wba place_unemplyr estsamp==1 , nocons dev
matrix R = corr(R)

local n_obs: di r(N)

frame change corr_matrix

svmat R, names(col)

replace neigh_char = "pctwhtp" if pctwhtp == 1
replace neigh_char = "place_povrate" if place_povrate == 1
replace neigh_char = "place_medinc" if place_medhhincome == 1
replace neigh_char = "place_wba" if place_wba == 1
replace neigh_char = "place_unemplyr" if place_unemplyr == 1


	// make table 

levelsof neigh_char, local(neigh_char)
levelsof pctwhtp, local(outcome)
levelsof place_povrate, local(place_povrate)
levelsof place_medhhincome, local(place_medhhincome)
levelsof place_wba, local(place_wba)
levelsof place_unemplyr, local(place_unemplyr)


foreach char in `neigh_char' {
    local id1 = "`char'"
				
				sum pctwhtp if neigh_char == "`char'" /// 
						
							local `id1'_pctyt = string(r(mean), "%9.3fc")
				
				sum place_povrate if neigh_char == "`char'" /// 
						
							local `id1'_povrate = string(r(mean), "%9.3fc")
							
				sum place_medhhincome if neigh_char == "`char'" /// 
						
							local `id1'_medinc = string(r(mean), "%9.3fc")
				
				sum place_wba if neigh_char == "`char'" /// 
						
							local `id1'_wba = string(r(mean), "%9.3fc")
							
				sum place_unemplyr if neigh_char == "`char'" /// 
						
							local `id1'_unemply = string(r(mean), "%9.3fc")
				
		}
	
	


texdoc init "$results\Supplementary_Table_2.tex", replace force
tex \resizebox{\textwidth}{!}{
tex \begin{tabular}{lccccccccc} \toprule \\[-1.5ex]
tex &  \multicolumn{9}{c}{Placement Tract Characteristics}  \\ \\[-1.75ex]
tex & \multicolumn{1}{c}{\parbox{1.55cm}{\center \% White}} & & \multicolumn{1}{c}{\parbox{1.55cm}{\center Poverty Rate}} & & \multicolumn{1}{c}{\parbox{1.55cm}{\center Median Income}} & & \multicolumn{1}{c}{\parbox{1.55cm}{\center BA+}} & & \multicolumn{1}{c}{\parbox{1.55cm}{\center Unemploy-ment}} \\
tex & (1) &  & (2) & &(3)& &(4)& & (5)  \\ \\[-1.75ex]
tex \cmidrule{2-10}  \\ \\[-1.75ex]   
tex \% White         &`pctwhtp_pctyt' &   &  & &  & &  & &  \\
tex Poverty Rate      &`pctwhtp_povrate' &   & `place_povrate_povrate' & &  & &  & &  \\
tex Median Income           &`pctwhtp_medinc' &   & `place_povrate_medinc' & & `place_medinc_medinc' & &  & &  \\
tex BA+      &`pctwhtp_wba' &   & `place_povrate_wba' & & `place_medinc_wba' & & `place_wba_wba' & &  \\
tex Unemployment    &`pctwhtp_unemply' &   & `place_povrate_unemply' & & `place_medinc_unemply' & & `place_wba_unemply' & & `place_unemplyr_unemply' \\ \\[-1.5ex]
tex N & `n_obs'  & & `n_obs' & & `n_obs' & & `n_obs' & &`n_obs' \\ \\[-1.5ex]
tex \hline \hline
tex \end{tabular}%
tex }
texdoc close



